<?php
class Content_MenuController extends Areal_Controller_Action
{
    public function indexAction()
    {
        $menu_id = $this->getRequest()->getParam('menu_id');
        $languageRootMenu = Doctrine::getTable('Content_Model_Menu')
                          ->findOneByTitle(
                              strtoupper($this->getRequest()->getParam('language')));
        if ($menu_id){
            $menu = Doctrine::getTable('Content_Model_Menu')->find($menu_id);
            $this->view->assign('menu_children', $languageRootMenu->getNode()->getChildren());
            $this->view->assign('language_root_menu', $languageRootMenu->toArray());
            $this->view->assign('menu', $menu);
        } else {
            if ($languageRootMenu->getNode()->hasChildren()){
                $siblings = $languageRootMenu->getNode()->getChildren();
                $menu = $siblings[0];
                $this->_forward('index', 'menu', 'content', array('menu_id'=>$menu->id));
            }
        }
    }
    
    public function introAction()
    {
        $menu_id = $this->getRequest()->getParam('menu_id');
        $menu = Content_Model_Menu::getSiteMenuTree();
        $this->view->assign('menu', $menu);
    }
    
    public function adminAction()
    {
        $menu_id = $this->getRequest()->getParam('menu_id');
        $menu = Doctrine::getTable('Content_Model_Menu')->find($menu_id);
        $this->view->assign('menus', $menu->getAdminSubmenuList());
        $this->view->menu_id = $menu_id;

        // if menu edit form submited:
        if ($this->getRequest()->isPost()){
            if ($menu->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                //$parent = $menu->getNode()->getParent();
                return $this->_helper->redirector('admin','menu','content',
                                                  array('menu_id'=>$menu['id']));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->editForm = $menu->getForm();
        $this->view->assign('menu_id', $menu_id);
        Content_Model_Menu::addMenusToNavigation($this->view->navigation(), $menu_id);
    }

    public function menuAction()
    {
        $rootCategory = Catalog_Model_Category::getCategoryMenuTree();
        $this->view->assign('rootCategory', $rootCategory);
    }

    public function adminMenuAction()
    {
        $rootCategory = Content_Model_Menu::getAdminMenuTree();
        $this->view->assign('rootCategory', $rootCategory);
    }

    public function addAction()
    {
        $parent_id = $this->getRequest()->getParam('parent_id');
        $menu = new Content_Model_Menu();
        if ($this->getRequest()->isPost()){
            if ($menu->add($this->getRequest()->getPost(), $parent_id)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','menu','content',
                                                  array('menu_id'=>$parent_id));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $menu->getForm();
        $this->view->assign('menu_id', $parent_id);
        Content_Model_Menu::addMenusToNavigation($this->view->navigation(), $parent_id);
    }

    public function moveAction()
    {
        $menu_id = $this->getRequest()->getParam('menu_id');
        $menu = Doctrine::getTable('Content_Model_Menu')->find($menu_id);
        if ($this->getRequest()->isPost()){
            if ($menu->move($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu moved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $parent = $menu->getNode()->getParent();
                return $this->_helper->redirector('admin','menu','content',
                                                  array('menu_id'=>$parent['id']));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu could not be moved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $menu->getMoveForm();
        $this->view->assign('menu_id', $menu_id);
        Content_Model_Menu::addMenusToNavigation($this->view->navigation(), $menu_id);
    }

    public function deleteAction()
    {
        $menu_id = $this->getRequest()->getParam('menu_id');
        $menu = Doctrine::getTable('Content_Model_Menu')->find($menu_id);
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $parent = $menu->getNode()->getParent();
                $menu->getNode()->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Menu deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','menu','content',
                                                  array('menu_id'=>$parent['id']));
            }
        }
        $this->view->form = $form;
        $this->view->assign('menu_id', $menu_id);
        Content_Model_Menu::addMenusToNavigation($this->view->navigation(), $menu_id);

    }
}
